<?php
/**
 *
 * @revision:   $Id: vodcategories.php 1.0.0 2011-05-11 17:30:20 ALEXEY GORDEYEV IK $
 * @package	    Joomla!
 * @since       {SINCE}
 * @subpackage	AG 3D Carousel
 * @version     {PROJECTVERSION}
 * @desc        Image Carousel module based on ImageFlow object-oriented script.
 * @copyright	Copyright © 2010-{PROJECTYEAR} Alexey Gordeyev IK. All rights reserved.
 * @license		GNU General Public License v2.0
 * @author		ALEXEY GORDEYEV IK
 * @author mail	admin@gordejev.lv
 * @website		www.gordejev.lv
 *
 * CODE GENERATED BY: ALEXEY GORDEYEV IK CODE GENERATOR
 * HTTP://WWW.AGJOOMLA.COM/
 *
 *
 */

// access denied
defined('JPATH_BASE') or die();

class JElementVodCategories extends JElement
{
   // name of element
   var $_name = 'VodCategories';
   // Construct an array of the HTML OPTION statements.
   var $_options = array();
   // function to create an element
   function fetchElement($name, $value, &$node, $control_name)
   {
      // Base name of the HTML control.
      $ctrl  = $control_name .'['. $name .']';
      // creating database instance
      $db =& JFactory::getDBO();
      // generating query
      $db->setQuery("SELECT c.name AS name, c.id AS id, c.parent_category_id AS parent FROM #__ag_vod_categories AS c WHERE published = 1 ORDER BY c.name, c.parent_category_id ASC");
      // getting results
      $results = $db->loadObjectList();

      if(count($results)){
         // iterating
         $temp_options = array();
         	
         foreach ($results as $item)
         {
            array_push($temp_options, array($item->id, $item->name, $item->parent));
         }
         	
         foreach ($temp_options as $option)
        	{
        	   if($option[2] == 0)
        	   {
        	      $this->_options[] = JHTML::_('select.option', $option[0], JText::_($option[1]));
        	      $this->recursive_options($temp_options, 1, $option[0]);
        	   }
        	}
        	// Construct the various argument calls that are supported.
        	$attribs       = ' ';
        	if ($v = $node->attributes( 'class' )) $attribs .= 'class="'.$v.'"';
        	else $attribs .= 'class="inputbox"';
        	
        	if ($v = $node->attributes( 'size' )) $attribs .= 'size="'.$v.'"';

        	// if multiselection enabled
        	if ($m = $node->attributes( 'multiple' ))
        	{
        	   $attribs .= ' multiple="multiple"';
        	   $ctrl .= '[]';
        	}
        	
        	array_unshift($this->_options, JHTML::_('select.option', '0', '- '.JText::_('MOD_AGCAROUSEL_SELECT_VOD_CATEGORY').' -', 'value', 'text'));
        	
        	// Render the HTML SELECT list.
        	return JHTML::_('select.genericlist', $this->_options, $ctrl, $attribs, 'value', 'text', $value, $control_name.$name );
      }
      else
      {
         // Render the HTML SELECT list.
         return '<strong>VoD is not installed or any VoD categories are available.</strong><input id="paramsvod_categories" type="hidden" name="'.$control_name.$name.'" value="-1" />';
      }
   }
   // bind function to save
   function bind( $array, $ignore = '' )
   {
      if (key_exists( 'field-name', $array ) && is_array( $array['field-name'] )) {
        	$array['field-name'] = implode( ',', $array['field-name'] );
      }

      return parent::bind( $array, $ignore );
   }

   function recursive_options($temp_options, $level, $parent){
      foreach ($temp_options as $option)
      {
         if($option[2] == $parent)
         {
            $level_string = '';
            for($i = 0; $i < $level; $i++) $level_string .= '&mdash;&mdash;';
            $this->_options[] = JHTML::_('select.option', $option[0], JText::_($level_string . $option[1]));

            $this->recursive_options($temp_options, $level+1, $option[0]);
         }
      }
   }
}